package com.ants.modules.ArticleManage.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.ants.common.system.api.ISysBaseAPI;
import com.ants.common.system.vo.LoginUser;
import com.ants.modules.ArticleManage.dto.ArticleManageDto;
import com.ants.modules.ArticleManage.dto.SysUserVo;
import com.ants.modules.ArticleManage.entity.ArticleManage;
import com.ants.modules.ArticleManage.mapper.ArticleLikeCollectionMapper;
import com.ants.modules.ArticleManage.service.ArticleLikeCollectionService;
import com.ants.modules.ArticleManage.service.ArticleManageService;
import com.ants.modules.ArticleManage.mapper.ArticleManageMapper;
import com.ants.modules.ArticleManage.vo.ArticleManageVo;
import com.ants.modules.articleFavorites.mapper.ArticleFavoritesSubMapper;
import com.ants.modules.articleLable.entity.ArticleLable;
import com.ants.modules.articleLable.service.ArticleLableService;
import com.ants.modules.articleSort.entity.ArticleSort;
import com.ants.modules.articleSort.service.ArticleSortService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * TODO
 * Author Chen
 * Date   2021/9/7 18:44
 */
@Service
public class ArticleManageServiceImpl extends ServiceImpl<ArticleManageMapper, ArticleManage> implements ArticleManageService {

    @Autowired
    ArticleManageMapper articleManageMapper;
    @Autowired
    ArticleLikeCollectionMapper articleLikeCollectionMapper;
    @Autowired
    ArticleFavoritesSubMapper articleFavoritesSubMapper;

    @Autowired
    ArticleLableService articleLableService;
    @Autowired
    ArticleSortService articleSortService;
    @Autowired
    ArticleLikeCollectionService articleLikeCollectionService;
    @Autowired
    ISysBaseAPI sysBaseAPI;

    @Override
    public List<ArticleManageDto> getAllList() {
        return articleManageMapper.getAllList();
    }

    @Override
    public List<ArticleManageVo> initArticleLable(String username) {
        return articleManageMapper.initArticleLable(username);
    }

    @Override
    public List<Map<String, Object>> articleGroupByCreateTime(String username) {
        return articleManageMapper.articleGroupByCreateTime(username);
    }

    @Override
    public List<ArticleManage> getArticleByTime(String time, String username) {
        return articleManageMapper.getArticleByTime(time, username);
    }


    @Override
    public void updateIsIncluded(String isIncluded, String id) {
        articleManageMapper.updateIsIncluded(isIncluded, id);
    }

    @Override
    public List<Map<String, Object>> articleStatisticsLineChart(String username, String startTime, String endTime, String otherSql) {
        return articleManageMapper.articleStatisticsLineChart(username, startTime, endTime, otherSql);
    }

    @Override
    public List<Map<String, Object>> sortStatisticsChart() {
        return articleManageMapper.sortStatisticsChart();
    }

    @Override
    public int getViewNumSum(String username) {
        return articleManageMapper.getViewNumSum(username);
    }

    @Override
    public void updateViewsNum(String id) {
        articleManageMapper.updateViewsNum(id);
    }


    @Override
    public Integer beLiked(String username) {
        return articleManageMapper.beLiked(username);
    }


    @Override
    public List<ArticleManageDto> getPersonMyFavoritesList(String username, Integer offset, int pageSize) {
        return articleManageMapper.getPersonMyFavoritesList(username, offset, pageSize);
    }

    @Override
    public List<ArticleManageDto> getPersonLikeList(String username, Integer offset, int pageSize) {
        return articleManageMapper.getPersonLikeList(username, offset, pageSize);
    }

    @Override
    public List<ArticleManageDto> getArticleByUser(String username, Integer offset, int pageSize) {
        return articleManageMapper.getArticleByUser(username, offset, pageSize);
    }

    @Override
    public List<Map<String, Object>> queryHotSortBySize(Integer pageSize) {
        return articleManageMapper.queryHotSortBySize(pageSize);
    }

    @Override
    public Boolean updateArticleDelFlag(String id, int delFalg) {
        return articleManageMapper.updateArticleDelFlag(id, delFalg).equals(1);
    }

    @Override
    public List<ArticleManageDto> getArticleAllByUser(String username) {
        return articleManageMapper.getArticleAllByUser(username);
    }

    @Override
    public Integer getArticleLikeNumByUserNmae(String username) {
        return articleManageMapper.getArticleLikeNumByUserNmae(username);
    }

    @Override
    public Integer getArticleFavoritesNumByUserNmae(String username) {
        return articleManageMapper.getArticleFavoritesNumByUserNmae(username);
    }


    @Override
    public List<Map<String, Object>> getArticleTop5ByViewNum() {
        return articleManageMapper.getArticleTop5ByViewNum();
    }

    @Override
    public List<Map<String, Object>> getSortTop5() {
        return articleManageMapper.getSortTop5();
    }

    @Override
    public List<Map<String, Object>> recommend() {
        return articleManageMapper.recommend();
    }

    @Override
    public Map<String, String> getUserInfoByArticleId(String id) {
        return articleManageMapper.getUserInfoByArticleId(id);
    }

    @Override
    public ArticleManageDto getArticleToESById(String id) {
        ArticleManageDto map = articleManageMapper.getArticleToESById(id);
        if (ObjectUtil.isNull(map)) {
            return new ArticleManageDto();
        }
        genArticleManageDto(map);
        return map;
    }

    private void genArticleManageDto(ArticleManageDto articleManage) {
        String id = articleManage.getId();
        // 获取分类
        ArticleSort articleSort = articleSortService.getById(articleManage.getArticleSort());
        articleManage.setArticleSort_dictText(articleSort.getName());
        articleManage.setSort(articleSort);
        // 获取标签
        String[] lables = articleManage.getArticleLable().split(",");
        List<ArticleLable> articleLableList = articleLableService.listByIds(Arrays.asList(lables));
        articleManage.setTags(articleLableList);
        List<String> lableNames = articleLableList.stream().map(ArticleLable::getName).collect(Collectors.toList());
        articleManage.setArticleLable_dictText(StringUtils.join(lableNames, ","));
        // 获取用户
        String username = articleManage.getCreateBy();
        LoginUser user = sysBaseAPI.getUserByName(username);
        SysUserVo userVo = new SysUserVo();
        BeanUtils.copyProperties(user, userVo);
        articleManage.setUser(userVo);

        // 获取文章点赞用户
        List<Map<String, String>> like_list = articleLikeCollectionService.getWxArticleLikeUser(id);
        articleManage.setLikeList(like_list);

    }


    @Override
    public List<Map<String, Object>> getWxMyLikesArticle(String username, Integer offset) {
        return articleManageMapper.getWxMyLikesArticle(username, offset);
    }


    @Override
    public List<Map<String, Object>> getWxMyCollectArticle(String username, Integer offset) {
        return articleManageMapper.getWxMyCollectArticle(username, offset);
    }
}
